package com.my.attendance.controller;

import com.my.attendance.entity.Attendance;
import com.my.attendance.entity.Employee;
import com.my.attendance.service.UserService;
import sun.nio.ch.SelectorImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@WebServlet("/go")
public class GoWorkServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            Employee employee = (Employee) req.getSession().getAttribute("userinfo");
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date day = simpleDateFormat.parse(simpleDateFormat.format(date));
            SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");
            String start_type="未签到";
            Date start_time = format1.parse(format1.format(date));
            Attendance attendance = new Attendance(employee.getEmployee_number(),day,start_type);
            UserService userService = new UserService();
            userService.gowork(attendance);
            req.getSession().setAttribute("userinfo",employee);
            req.getRequestDispatcher("/jsp/admin/home.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
